﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StartReading.Security.Validation
{
    class MaximumStringLengthValidation : IValidation<String, int, String>
    {
        private MaximumStringLengthValidation()
        {
        }

        #region Lazy Singleton Thread-Safe
        private static class MaximumStringLengthValidationHolder
        {
            static MaximumStringLengthValidationHolder()
            {
            }

            internal static readonly MaximumStringLengthValidation MaximumStringLengthValidation = new MaximumStringLengthValidation();
        }

        /// <summary>
        /// Gets an instance of MaximumStringLengthValidation class
        /// </summary>
        public static MaximumStringLengthValidation Instance
        {
            get { return MaximumStringLengthValidationHolder.MaximumStringLengthValidation; }
        }
        #endregion

        /// <summary>
        ///     Validates maximum length of string.
        ///     Throws ArgumentException if the maximum length is not met.
        /// </summary>
        /// <param name="item">
        ///     String to be validated
        /// </param>
        /// <param name="maximumLength">
        ///     Maximum Length
        /// </param>
        /// <param name="itemName">
        ///     Name of validated string for exception message
        /// </param>
        /// <exception cref="System.ArgumentException"></exception>
        public void IsValid( string item, int maximumLength, string itemName )
        {
            if ( item.Length > maximumLength )
                throw new ArgumentException( String.Format( "Maximum {0} length is {1}", itemName, maximumLength ) );
        }
    }
}
